from flask import Blueprint, render_template, request

from applications.common.curd import model_to_dicts, enable_status, disable_status
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape

from applications.models import PoliceModels
from applications.schemas import PoliceOutSchema

police_monitor = Blueprint('PoliceMonitor', __name__, url_prefix='/admin/police/monitor')
##TODO
@police_monitor.get('/')
def police_monitor_main():
    return render_template('/admin/police/monitor.html')

#   用户分页查询
@police_monitor.get('/data')
def data():

    # 获取请求参数
    id = request.args.get('code', type=int)
    name = xss_escape(request.args.get('name', type=str))
    state = xss_escape(request.args.get('state', type=int))
    score = xss_escape(request.args.get('score', type=int))
    department = xss_escape(request.args.get('department', type=str))
    department = xss_escape(request.args.get('department', type=str))
    department = xss_escape(request.args.get('department', type=str))
    department = xss_escape(request.args.get('department', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if name:
        mf.contains(field_name="name", value=name)
    if Occur:
        mf.contains(field_name="Occur", value=Occur)
    if Method:
        mf.contains(field_name="Method", value=Method)
    if Score:
        mf.contains(field_name="Score", value=Score)


    # orm查询
    # 使用分页获取data需要.items
    credible_fire = Credible_Fire.query.filter(mf.get_filter(model=Credible_Fire)).layui_paginate()
    count = credible_fire.total
    # 返回api
    return table_api(data=model_to_dicts(schema=Credible_FireOutSchema, data=credible_fire.items), count=count)


